package com.titleevaluation.controller;

import com.titleevaluation.entity.Punishments;
import com.titleevaluation.service.PunishmentsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/punishments")
public class PunishmentsController {

    private final PunishmentsService punishmentsService;

    public PunishmentsController(PunishmentsService punishmentsService) {
        this.punishmentsService = punishmentsService;
    }

    @PostMapping
    public ResponseEntity<Punishments> createPunishments(@PathVariable Long applicationId, @RequestBody Punishments punishments) {
        Punishments createdPunishments = punishmentsService.createPunishments(applicationId, punishments);
        return ResponseEntity.ok(createdPunishments);
    }

    @GetMapping
    public ResponseEntity<List<Punishments>> getPunishmentsByApplicationId(@PathVariable Long applicationId) {
        List<Punishments> punishments = punishmentsService.getPunishmentsByApplicationId(applicationId);
        return ResponseEntity.ok(punishments);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Punishments> getPunishmentsById(@PathVariable Long id) {
        return punishmentsService.getPunishmentsById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<Punishments> updatePunishments(@PathVariable Long id, @RequestBody Punishments punishments) {
        Punishments updatedPunishments = punishmentsService.updatePunishments(id, punishments);
        return ResponseEntity.ok(updatedPunishments);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePunishments(@PathVariable Long id) {
        punishmentsService.deletePunishments(id);
        return ResponseEntity.noContent().build();
    }
}

